//
//  MYGCommunityRecommendView.swift
//  MYGrow
//
//  Created by lance on 2024/2/29.
//

import UIKit

class MYGCommunityRecommendView: UIView {
    // MARK: - Property
    var cellModels: [MYGCommunityRecommendCellModel] = []
    var reloadButonTitle:(()->Void)?
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
        
    // MARK: - Public funcs
    public func reload(with cellModels: [MYGCommunityRecommendCellModel]) {
        self.cellModels = cellModels
        collectionView.reloadData()
    }

    // MARK: - lazy load
    private lazy var collectionView: UICollectionView = {
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.minimumLineSpacing = 14.5
        flowlayout.minimumInteritemSpacing = 20
        flowlayout.sectionInset = .init(top: 20, left: 25, bottom: 20, right: 25)
        
        let itemWidth = (screenWidth - 90) / 3
        let itemHeight = itemWidth + 10.5 + 15
        flowlayout.itemSize = CGSize(width: itemWidth, height: itemHeight)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: MYGCommunityRecommendCollectionViewCell.self)
        return collectionView
    }()
    
//    private lazy var collectionView: UICollectionView = {
//        let flowlayout = UICollectionViewFlowLayout()
//        flowlayout.minimumLineSpacing = 14.5
//        flowlayout.minimumInteritemSpacing = 20
//        flowlayout.sectionInset = .init(top: 20, left: 25, bottom: 20, right: 25)
//        let itemWidth = (screenWidth - 20 * 2 - 25 * 2) / 3
//        let itemHeight = itemWidth + 10.5 + 15
//        flowlayout.itemSize = CGSize(width: itemWidth, height: itemHeight)
//        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowlayout)
//        collectionView.backgroundColor = .clear
//        collectionView.delegate = self
//        collectionView.dataSource = self
//        collectionView.register(cellWithClass: MYGCommunityRecommendCollectionViewCell.self)
//        return collectionView
//    }()
}

extension MYGCommunityRecommendView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        cellModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGCommunityRecommendCollectionViewCell.self, for: indexPath)
        cell.model = cellModels[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        cellModels[indexPath.row].isSelected = !cellModels[indexPath.row].isSelected
        collectionView.reloadData()
        reloadButonTitle?()
    }
}
